package gitlab_hook

import (
	"encoding/json"
	"fmt"
	"testing"
)

var jobData = `
{
    "object_kind": "build",
    "ref": "master",
    "tag": false,
    "before_sha": "baf6f136bb54dca2b2790b975fc5f39ff0471762",
    "sha": "c2ac18263bd723883752d28d52427190851b2113",
    "build_id": 2539794,
    "build_name": "deploy1",
    "build_stage": "deploy",
    "build_status": "success",
    "build_created_at": "2022-04-16 19:58:55 +0800",
    "build_started_at": "2022-04-16 19:59:26 +0800",
    "build_finished_at": "2022-04-16 19:59:37 +0800",
    "build_duration": 10.841131,
    "build_queued_duration": 0.463871,
    "build_allow_failure": false,
    "build_failure_reason": "unknown_failure",
    "pipeline_id": 86232,
    "runner": {
        "id": 263,
        "description": "auto-scale-shared-runner-manager-3(16.4)",
        "runner_type": "instance_type",
        "active": true,
        "is_shared": true,
        "tags": [
            "shared",
            "docker",
            "gitlab-org"
        ]
    },
    "project_id": 20535,
    "project_name": "lingxi / Learn GitLab",
    "user": {
        "id": 5094,
        "name": "pan leiming",
        "username": "panleiming",
        "avatar_url": null,
        "email": "[REDACTED]"
    },
    "commit": {
        "id": 86232,
        "sha": "c2ac18263bd723883752d28d52427190851b2113",
        "message": "更新.gitlab-ci.yml文件",
        "author_name": "pan leiming",
        "author_email": "skyknight928@163.com",
        "author_url": "https://jihulab.com/panleiming",
        "status": "success",
        "duration": 40,
        "started_at": "2022-04-16 19:58:56 +0800",
        "finished_at": "2022-04-16 19:59:37 +0800"
    },
    "repository": {
        "name": "Learn GitLab",
        "url": "git@jihulab.com:lingxi/learn-gitlab.git",
        "description": "Learn how to use GitLab to support your software development life cycle.",
        "homepage": "https://jihulab.com/lingxi/learn-gitlab",
        "git_http_url": "https://jihulab.com/lingxi/learn-gitlab.git",
        "git_ssh_url": "git@jihulab.com:lingxi/learn-gitlab.git",
        "visibility_level": 0
    },
    "environment": null
}
`

func TestJobHook(t *testing.T) {
	hook := &JobHook{}
	err := json.Unmarshal([]byte(jobData), hook)
	if err != nil {
		t.Error(err)
		t.FailNow()
	}
	data, _ := json.MarshalIndent(hook, "  ", "  ")
	fmt.Println(string(data))
}
